package com.woniu.service.impl;

import com.woniu.dao.UserMapper;
import com.woniu.entity.User;
import com.woniu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.annotation.Annotation;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    // 0代表用户名不存在，1代表密码错误，’2‘代表正确；
    @Override
    public Integer login(String name, String password) {
        User user=new User();
        user.setName(name);
        User userResult = userMapper.selectOne(user);
        if(userResult==null){
            return 0;
        }
        else {
            String realPassword = userResult.getPassword();
            if(realPassword.equals(password)){
                return 2;
            }else {
                return 1;
            }
        }
    }

    @Override
    public Integer register(User user) {
        return userMapper.insert(user);
    }

    @Override
    public String confirmExist(String username, String tel, String email) {
        User user=new User();
        user.setName(username);
        User conformUser=userMapper.selectOne(user);
        String result=null;
        if(conformUser!=null){
            result="用户名重复";
        }
        user=new User();
        user.setTelephone(tel);
        conformUser=userMapper.selectOne(user);
        if(conformUser!=null){
            result="电话已使用";
        }
        user=new User();
        user.setEmail(email);
        conformUser=userMapper.selectOne(user);
        if(conformUser!=null){
            result="邮箱已使用";
        }
        return result;
    }

    @Override
    public Long getUserId(String userName) {
        User user = new User();
        user.setName(userName);
        user=userMapper.selectOne(user);
        return user.getId();
    }


}
